package com.boc.langxiang.presenter.me;

import com.boc.langxiang.contract.me.OrdersDetailContract;
import com.boc.langxiang.entity.request.CancelOrdersRequest;
import com.boc.langxiang.entity.request.ConfirmReceiptRequest;
import com.boc.langxiang.entity.request.OrdersDetailReturnOrdersInfoRequest;
import com.boc.langxiang.entity.request.OrdersDetailWaitDeliveryRequest;
import com.boc.langxiang.entity.request.OrdersDetailWaitPayRequest;
import com.boc.langxiang.entity.request.OrdersDetailWaitReceiveRequest;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.OrdersDetailReturnProductContent;
import com.boc.langxiang.entity.response.OrdersDetailWaitDeliveryContent;
import com.boc.langxiang.entity.response.OrdersDetailWaitPayContent;
import com.boc.langxiang.entity.response.OrdersDetailWaitReceiveContent;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class OrdersDetailPresenter implements OrdersDetailContract.Presenter {

    private final ApiService mService;
    private OrdersDetailContract.View mView;

    public OrdersDetailPresenter(OrdersDetailContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        this.mView = view;
    }

    /**
     * 待收货详情
     *
     * @param id
     */
    @Override
    public void toReceiptOrdersInfo(String id) {
        mView.showLoading();
        OrdersDetailWaitReceiveRequest request = new OrdersDetailWaitReceiveRequest();
        request.id = id;
        mService.toReceiptOrdersInfo(request.params()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<OrdersDetailWaitReceiveContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<OrdersDetailWaitReceiveContent> response) {
                        OrdersDetailWaitReceiveContent content = response.getContent();
                        mView.getReceiptOrdersInfo(content);
                    }
                });
    }

    /**
     * 退货详情
     *
     * @param id
     */
    @Override
    public void returnOrdersInfo(String id) {
        mView.showLoading();
        OrdersDetailReturnOrdersInfoRequest request = new OrdersDetailReturnOrdersInfoRequest();
        request.id = id;
        mService.returnOrdersInfo(request.params()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<OrdersDetailReturnProductContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<OrdersDetailReturnProductContent> response) {
                        OrdersDetailReturnProductContent content = response.getContent();
                        mView.getReturnOrdersInfo(content);
                    }
                });
    }


    /**
     * 待付款详情
     *
     * @param id
     */
    @Override
    public void toPayOrdersInfo(String id) {

        mView.showLoading();
        OrdersDetailWaitPayRequest request = new OrdersDetailWaitPayRequest();
        request.id = id;
        mService.toPayOrdersInfo(request.params()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<OrdersDetailWaitPayContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<OrdersDetailWaitPayContent> response) {
                        OrdersDetailWaitPayContent content = response.getContent();
                        mView.getPayOrdersInfo(content);
                    }
                });

    }

    /**
     * 待发货详情
     *
     * @param id
     */
    @Override
    public void toSendOrdersInfo(String id) {
        mView.showLoading();
        OrdersDetailWaitDeliveryRequest request = new OrdersDetailWaitDeliveryRequest();
        request.id = id;
        mService.toSendOrdersInfo(request.params()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<OrdersDetailWaitDeliveryContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<OrdersDetailWaitDeliveryContent> response) {
                        OrdersDetailWaitDeliveryContent content = response.getContent();
                        mView.getSendOrdersInfo(content);
                    }
                });
    }

    /**
     * 取消订单
     *
     * @param id
     */
    @Override
    public void cancelOrders(String id) {
        mView.showLoading();
        CancelOrdersRequest cancelOrdersRequest = new CancelOrdersRequest();
        cancelOrdersRequest.id = id;
        Observable<BaseResponse> cancelOrders = mService.cancelOrders(cancelOrdersRequest.params());
        cancelOrders.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse>(mView) {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        mView.cancelOrdersSuccess();
                    }
                });
    }

    /**
     * 确认收货
     *
     * @param id
     */
    @Override
    public void confirmReceipt(String id) {
        mView.showLoading();
        ConfirmReceiptRequest request = new ConfirmReceiptRequest();
        request.id = id;
        Observable<BaseResponse> confirmReceipt = mService.confirmReceipt(request.params());
        confirmReceipt.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse>(mView) {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        mView.conFirmReceiptSuccess();
                    }
                });
    }

}
